from fastapi import FastAPI
from typing import Optional
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles

app = FastAPI()
app.mount('/static', StaticFiles(directory='static'))


class CityInfo(BaseModel):
    province: str
    country: str
    is_affected: Optional[bool] = None  # bool的区别是可以不传，默认是null


#
# @app.get("/")
# def hello_word():
#     return {"hello": "world"}
#
#
# @app.get("/city/{city}")
# # http://127.0.0.1:8000/city/beijing?query_string=hongcongjin
# def result(city: str, query_string: Optional[str] = None):
#     return {"city": city, "query_string": query_string}
#
#
# @app.put("/city/{city}")
# def result(city: str, city_info: CityInfo):
#     return {"city": city, "country": city_info.country, "is_affected": city_info.is_affected}

@app.get("/")
async def hello_word():
    return {"hello": "world"}


@app.get("/city/{city}")
# http://127.0.0.1:8000/city/beijing?query_string=hongcongjin
async def result(city: str, query_string: Optional[str] = None):
    return {"city": city, "query_string": query_string}


@app.put("/city/{city}")
async def result(city: str, city_info: CityInfo):
    return {"city": city, "country": city_info.country, "is_affected": city_info.is_affected}

# 启动命令：uvicorn hello_world:app --reload
